package com.group01.community.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.ZyOwner;
import com.group01.community.entity.ZyRepair;
import com.group01.community.entity.vo.ZyRepairVo;
import com.group01.community.service.ZyRepairService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;

/**
 * <p>
 * 报修信息 前端控制器
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Api(tags = "报修信息")
@RestController
@RequestMapping("/zyRepair")
public class ZyRepairController {

    @Autowired
    private ZyRepairService zyRepairService;

    @ApiOperation("查询所有报修信息")
    @PostMapping("/getZyRepairsByPage")
    public ResponseResult<IPage<ZyRepairVo>> getZyRepairs(@RequestBody ZyRepairVo zyRepair) {

        IPage<ZyRepairVo> zyRepairVoIPage = zyRepairService.selectZyRepairPage(zyRepair);
        return ResponseResult.ok(zyRepairVoIPage);
    }

    /**
     * 小程序
     * - 新增报事报修
     * - 报事报修列表
     * - 报事报修进度追踪
     */
    @ApiOperation(value = "新增报修 " ,notes="注意 需要小区id 和user_id  ，user_id=业主id")
    @PostMapping("/add")
    public ResponseResult<String> addComplaintSuggest(@RequestBody ZyRepair zyRepair) {
        zyRepair.setRepairNum(UUIDUtils.getRepairCode());
        zyRepair.setRepairState("Pending"); //待处理
        zyRepair.setAssignmentTime(LocalDateTime.now());
        zyRepair.setReceivingOrdersTime(LocalDateTime.now());
        zyRepair.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        zyRepair.setCreateTime(LocalDateTime.now());
        zyRepair.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        zyRepair.setUpdateTime(LocalDateTime.now());
        zyRepair.setDelFlag(0);

        zyRepairService.save(zyRepair);
        return ResponseResult.ok("ok");
    }


//    @ApiOperation(" 报修 进度追踪" )
//    @GetMapping("/get")
//    public ResponseResult<String> getComplaintSuggestStatus() {
//
//
//        return ResponseResult.ok("ok");
//    }

    @ApiOperation("报事报修进度追踪")
    @PutMapping("/update")
    public ResponseResult<String> updateComplaintSuggestStatus(@RequestBody ZyRepair zyRepair) {

        if (zyRepair.getRepairState().equals("Cancelled")){
            zyRepair.setCancelTime(LocalDateTime.now());
        }
        if (zyRepair.getRepairState().equals("Processed")){
            zyRepair.setCompleteTime(LocalDateTime.now());
        }
        zyRepairService.updateById(zyRepair);
        return ResponseResult.ok("ok");
    }


    @ApiOperation("报事报修进度追踪")
    @PutMapping("/updateProcess")
    public ResponseResult<String> updateProcess(@RequestBody ZyRepair zyRepair) {

        LambdaUpdateWrapper<ZyRepair> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ZyRepair::getRepairId, zyRepair.getRepairId());

        updateWrapper.set(ZyRepair::getRepairState, zyRepair.getRepairState());

        if (zyRepair.getRepairState().equals("Allocated")){
            updateWrapper.set(ZyRepair::getAssignmentTime, LocalDateTime.now());
        }

        if (zyRepair.getRepairState().equals("Cancelled")){
            //zyRepair.setCancelTime(LocalDateTime.now());
            updateWrapper.set(ZyRepair::getCancelTime, LocalDateTime.now());
        }
        if (zyRepair.getRepairState().equals("Processed")){
            zyRepair.setCompleteTime(LocalDateTime.now());
            updateWrapper.set(ZyRepair::getCompleteTime, LocalDateTime.now());
        }


        zyRepairService.update(updateWrapper);
        return ResponseResult.ok("ok");
    }






}

